﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CuaHangMayTinh.PresentationLayer
{
    public partial class FormNhatKyLog : Form
    {
        string name = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\logs\";
        public FormNhatKyLog()
        {
            InitializeComponent();
        }

        private void FormNhatKyLog_Load(object sender, EventArgs e)
        {
            var dir = new DirectoryInfo(name);
//            var dir = Directory.GetFiles(name,"*.log");
            DataTable dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("FileName");
            dt.Columns.Add("DateTime");
            int i = 0;
            foreach (var fileInfo in dir.GetFiles("*.log"))
            {
                i++;
                dt.Rows.Add(new[]
                {
                    i.ToString(),
                    fileInfo.Name,
                    string.Format("{0:h:mm:ss tt}",File.GetCreationTime(name+fileInfo.Name))
                });
            }
            dataGridView_select.DataSource = dt;
        }

        private void toolStripBtn_xem_Click(object sender, EventArgs e)
        {
            if (dataGridView_select.SelectedRows.Count > 0)
            {
                string filename = (string)dataGridView_select.SelectedRows[0].Cells[1].Value;
                Process.Start("notepad.exe", name + filename);
            }
        }

        private void toolStripBtn_Xoa_Click(object sender, EventArgs e)
        {
            var dataTable = dataGridView_select.DataSource as DataTable;
            if (dataTable != null)
            {
                DataTable dt = dataTable.Clone();
                string pattern = "*.*";
                var matches = Directory.GetFiles(name, pattern);
                foreach(string file in Directory.GetFiles(name, pattern))
                    File.Delete(file);
                dataGridView_select.DataSource = dt;
            }
        }
    }
}
